<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Company;
use App\Models\ChinaAreaModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class CompanyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Company(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('companyname');
            $grid->column('companycate');
            $grid->column('companytype');
            $grid->column('companyworker');
            $grid->column('mastername');
            $grid->column('address');
            $grid->column('tel');
            $grid->column('content');
            $grid->thumb->image('/uploads', 50, 50);
            $grid->column('isrecommand', '是否推荐')->switch();
            $grid->column('city','所属城市')->display(function ($id){
                return ChinaAreaModel::getName($id);
            });
            $grid->column('area','所属区域')->display(function ($id){
                return ChinaAreaModel::getName($id);
            });
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Company(), function (Form $form) {
            $form->display('id');
            $form->select('province','省份')->options('getProvince')->options(function ($id){
                if ($id){
                    $province = ChinaAreaModel::where('name',$id)->first();
                    if ($province){
                        return [$province->id => $province->name];
                    }
                    else{
                        return [0 => '请选择'];
                    }
                }else{
                    return [0 => "请选择"];
                }
            })->load('city', 'getDCity');
            $form->select('city', '城市')->load('area', 'getArea');
            $form->select('area', '区域');
            $form->text('companyname');
            $form->text('companycate');
            $form->text('companytype');
            $form->text('companyworker');
            $form->text('mastername');
            $form->text('address');
            $form->text('tel');
            $form->text('content');
            $form->file('thumb')->uniqueName()->autoUpload();
            $form->switch('isrecommand', '是否推荐');
        });
    }

    public function getAllCompany(){
        return (new \App\Models\Company())->getAllCompany();
    }
}
